<?php


namespace App\Services;
use App\Tools\JsonTools;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class OssQiNiuYun
{
    public static function ossQny($filePath){
        // 需要填写你的 Access Key 和 Secret Key
        $accessKey =config("qny.accessKey");
        $secretKey = config("qny.secretKey");
        $bucket = config("qny.bucket");
        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        // 生成上传 Token
        $token = $auth->uploadToken($bucket);
        // 上传到存储后保存的文件名
        $key = md5(time().rand(123456,99999)).".jpg";
        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath, null, 'application/octet-stream', true, null, 'v2');
        if(!empty($err)){
            $errors = JsonTools::JsonError($err->response->statusCode);
            return  $errors;
        }
        return config("qny.http").$key;
    }

    /**
     * @Notes:
     * @Author:Planc,
     * @Date: 2022/11/18,
     * @Time: 19:50,
     * 删除图片
     */
    public static function ossQnyDelete($key){

        $accessKey =config("qny.accessKey");
        $secretKey = config("qny.secretKey");
        $bucket = config("qny.bucket");
        $auth = new Auth($accessKey, $secretKey);
        $config = new \Qiniu\Config();
        $bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
        $err = $bucketManager->delete($bucket, $key);
        if ($err) {
            return true;
        }
        return false;

    }

}
